<template>
  <van-pull-refresh class="home" v-model="loading" @refresh="onRefresh">
    <van-list
      v-model:loading="loadingMore"
      :finished="finished"
      finished-text="没有更多了~"
      @load="onLoad"
    >
      <div class="banner">
        <div class="company" @click="toCompany">
          <span>{{ company.$state.detail?.companyName }}</span>
          <span>详情 <van-icon name="arrow" /></span>
        </div>
        <van-swipe
          class="my-swipe"
          indicator-color="white"
          v-if="bannerList.length > 0"
        >
          <van-swipe-item v-for="i in bannerList" @click="toBannerInfo(i)">
            <van-image fit="cover" :src="host + i.imageUrl" />
          </van-swipe-item>
        </van-swipe>
      </div>

      <div class="list">
        <div v-for="i in dataList" :key="i.id">
          <div class="list_item" @click="toInfo(i)">
            <div class="content">
              <div class="title">
                {{ i.title }}
              </div>
              <div class="time">{{ i.createTime }}</div>
            </div>
            <van-image fit="cover" :src="host + i.homepageImage" />
          </div>
        </div>
      </div>
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref } from "vue";
import {
  queryHomePageRichTextList,
  queryHomePageCarouselImageList,
} from "@/api/index";
import { useRouter } from "vue-router";
import useCompanyStore from "../../store/modules/company";
let company = useCompanyStore();
company.setCompanyDetail()
let host = import.meta.env.VITE_APP_BASE_API;
let router = useRouter();
let loading = ref(true);
function onRefresh() {
  loading.value = true;
  finished.value = false;
  loadingMore.value = true;
  dataList.value = [];
  page.value.pageNum = 1;
  getList();
  getBanner();
}
// 获取首页数据
let page = ref({
  pageNum: 1,
  pageSize: 10,
});
let dataList = ref([]);
function getList() {
  queryHomePageRichTextList(page.value)
    .then((res) => {
      loading.value = false;
      loadingMore.value = false;
      dataList.value.push(...res.rows);
      if (res.rows.length < page.value.pageSize) {
        finished.value = true;
      }
    })
    .catch((err) => {
      loading.value = false;
    });
}
// 加载更多
let loadingMore = ref(true);
let finished = ref(false);
function onLoad() {
  page.value.pageNum += 1;
  getList();
}
getList();

// 获取首页轮播图
let bannerList = ref([]);
function getBanner() {
  queryHomePageCarouselImageList().then((res) => {
    bannerList.value = res.data;
  });
}
getBanner();
function toCompany() {
  router.push("/companyInfo");
}
function toInfo(row) {
  router.push("/txtInfo/" + row.id);
}
function toBannerInfo(row) {
  if (row.linkType == 1 || row.linkType == 2) {
    router.push("/txtInfo/" + row.linkUrl);
  } else if (row.linkType == 5) {
    location.href = row.linkUrl;
  } else if (row.linkType == 3) {
    router.push("/queryDetails/" + row.linkUrl);
  } else if (row.linkType == 4) {
    router.push("/questionDetail/" + row.linkUrl);
  }
}
</script>

<style scoped lang="scss">
.home {
  padding-bottom: 50px;
  height: calc(100vh - 50px);
  overflow: auto;
}
.list_item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #666;
  font-size: 14px;
  padding: 10px;
  border-bottom: 1px solid #eee;
  .content {
    padding: 10px;
    width: 230px;
    .title {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-bottom: 10px;
      color: #333;
    }
  }
  .van-image {
    width: 100px;
    height: 60px;
    border-radius: 4px;
    overflow: hidden;
  }
}
.company {
  background: var(--el-color-primaryop9);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 15px;
  padding: 10px 10px;
  .van-icon {
    transform: translateY(1px);
  }
  .detail_btn {
    border: 1px solid white;
    border-radius: 10px;
    padding: 1px 6px;
    margin-left: 10px;
  }
}
.banner {
  height: 200px;
  border-bottom: 1px solid #eee;
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  height: 160px;
  text-align: center;
  background-color: #39a9ed;
  .van-image {
    width: 100%;
    height: 100%;
  }
}
.bn_img {
  width: 150px;
  height: 50px;
}
</style>
